'''
Author: ytj
Date: 2024-02-19 10:22:56
LastEditTime: 2024-02-19 10:24:30
LastEditors: ytj
Description: 清理本地文件测试程序
'''
# -*- coding: UTF-8 -*-
# AI服务器源文件清理程序

import datetime
import os
import time

# from flask.scaffold import F

from lj_tool import tool_log


# 文件清理

# 时间差


def days(str1, str2):
    try:
        date1 = datetime.datetime.strptime(str1[0:8], "%Y%m%d")
        date2 = datetime.datetime.strptime(str2[0:8], "%Y%m%d")
        num = (date1-date2).days
    except:
        # 时间异常的直接删除
        num = 4
    return num


def clear_local_file():
    # 清理本地文件
    tool_log.info(f"开始删除本地文件")
    base_dir = "temp/"
    local_files = [os.path.join(file) for file in os.listdir(base_dir)]
    for local_file in local_files:
        file_name_list = local_file.split("-")

        if len(file_name_list) == 1:  # 原图片
            file_date = file_name_list[0]
        elif len(file_name_list) > 3:  # 分析文件
            file_date = file_name_list[2]

        # 判断时间，超过三天清理
        if days(datetime.datetime.now().strftime("%Y%m%d%H%M%S"), file_date) > 3:
         # 删除执行过的文件
            tool_log.info(f"删除本地文件-{base_dir}{local_file}")
            os.remove(f"{base_dir}{local_file}")
            time.sleep(0.1)


# 运行
clear_local_file()

# tool_run.do_everyday(clear_local_file, "01:00")
# tool_run.start()